package org.mespilus.mespilusweb.datalayer.usermanagement;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.mespilus.mespilusweb.datalayer.PMF;
import org.mespilus.mespilusweb.ui.usermanagement.client.model.FullMember;
import org.mespilus.mespilusweb.ui.usermanagement.client.model.UsefulMember;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Member {

	@PrimaryKey
	@Persistent
	protected Key keyPerson;

	@Persistent
	protected String mail;

	@Persistent
	protected String hash;

	@Persistent
	protected boolean mustChangePasswd;

	@Persistent
	protected String name;

	@Persistent
	protected String firstName;

	@Persistent
	protected String secondName;

	@Persistent
	protected String NIF;

	@Persistent
	protected String street;

	@Persistent
	protected String postalCode;

	@Persistent
	protected String city;

	@Persistent
	protected String country;

	@Persistent
	protected String phone1;

	@Persistent
	protected String phone2;

	@Persistent
	protected Key basket;

	public Member() {
		super();
	}

	public Key getBasket() {
		return basket;
	}

	public void setBasket(Key basket) {
		this.basket = basket;
	}

	public Key getKeyPerson() {
		return keyPerson;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

	public boolean getMustChangePasswd() {
		return mustChangePasswd;
	}

	public void setMustChangePasswd(boolean mustChangePasswd) {
		this.mustChangePasswd = mustChangePasswd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSecondName() {
		return secondName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getNIF() {
		return NIF;
	}

	public void setNIF(String nIF) {
		NIF = nIF;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getPhone1() {
		return phone1;
	}

	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public Member(FullMember fullMember) {
		mail = fullMember.getMail();
		name = fullMember.getName();
		city = fullMember.getCity();
		country = fullMember.getCountry();
		firstName = fullMember.getFirstName();
		// mustChangePasswd=person.getMustChangePasswd().equ ?;
		NIF = fullMember.getNIF();
		phone1 = fullMember.getPhone1();
		phone2 = fullMember.getPhone2();
		postalCode = fullMember.getPostalCode();
		secondName = fullMember.getSecondName();
		street = fullMember.getStreet();
		keyPerson = KeyFactory.createKey(Member.class.getSimpleName(), "_"
				+ mail);
	}

	public static Member getMember(String mail) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(Member.class.getSimpleName(), "_" + mail);
		return pm.getObjectById(Member.class, k);
	}
	
	public static void delete(String mail) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(Member.class.getSimpleName(), "_" + mail);
		Member memberToDelete = pm.getObjectById(Member.class, k);
		pm.deletePersistent(memberToDelete);
	}

	public static void delete(Member member){
		delete(member.getMail());		
	}
	
	public static void update(Member modifiedMember){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(Member.class.getSimpleName(), "_" + modifiedMember.getMail());
		Member storedMember = pm.getObjectById(Member.class, k);
		storedMember.setBasket(modifiedMember.getBasket());
		storedMember.setCity(modifiedMember.getCity());
		storedMember.setCountry(modifiedMember.getCountry());
		storedMember.setFirstName(modifiedMember.getFirstName());
		storedMember.setHash(modifiedMember.getHash());
		storedMember.setMail(modifiedMember.getMail());
		storedMember.setMustChangePasswd(modifiedMember.getMustChangePasswd());
		storedMember.setName(modifiedMember.getName());
		storedMember.setNIF(modifiedMember.getNIF());
		storedMember.setPhone1(modifiedMember.getPhone1());
		storedMember.setPostalCode(modifiedMember.getPostalCode());
		storedMember.setSecondName(modifiedMember.getSecondName());
		storedMember.setStreet(modifiedMember.getStreet());
		pm.close();
	}
	
	public static List<Member> getMembers() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Member.class);
		List<Member> entries = (List<Member>) query.execute();
		return entries;
	}

	public static FullMember getFullMember(UsefulMember usefulMember) {
		FullMember fullMember = new FullMember();
		Member member = getMember(usefulMember.getMail());
		fullMember.setMail(member.getMail());
		fullMember.setName(member.getName());
		fullMember.setCity(member.getCity());
		fullMember.setCountry(member.getCountry());
		fullMember.setFirstName(member.getFirstName());
		fullMember.setMustChangePasswd(member.getMustChangePasswd());
		fullMember.setNIF(member.getNIF());
		fullMember.setPhone1(member.getPhone1());
		fullMember.setPhone2(member.getPhone2());
		fullMember.setPostalCode(member.getPostalCode());
		fullMember.setSecondName(member.getSecondName());
		fullMember.setStreet(member.getStreet());
		return fullMember;
	}

	public static List<UsefulMember> getUsefulMembers() {
		List<UsefulMember> members = new ArrayList<UsefulMember>();
		List<Member> st_members = getMembers();
		for (Member p : st_members) {
			UsefulMember member = new UsefulMember();
			member.setMail(p.getMail());
			member.setName(p.getName());
			member.setFirstName(p.getFirstName());
			member.setPhone1(p.getPhone1());
			member.setPhone2(p.getPhone2());
			member.setSecondName(p.getSecondName());
			members.add(member);
		}
		return members;
	}

	public static void insert(Member member) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(member);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
	
	
	@Override
	public String toString() {
		String text;
		text = "Clau primaria: "+keyPerson.getName()+"\n";
		return text;
	}
}
